pluginHome = new File("./plugins").listFiles().find { it.name.startsWith('cloud-tools-')}
if(pluginHome == null) pluginHome = "."

includeTargets << new File("${pluginHome}/scripts/CloudToolsInit.groovy")

target('default': "Enable Elastic Block Store") {
    enableEbs()
}

target(enableEbs: "The implementation task") {
	depends( initCloudToolsService)
    cloudToolsService.withCluster { clusterManager, clusterName, cluster ->
		if(System.getProperty("cloudtools.ebs.volume.size") != null && System.getProperty("cloudtools.ebs.device") != null){
			
			event("StatusUpdate", ["Creating a new volume of size: ${System.getProperty("cloudtools.ebs.volume.size")}"])
			cluster.spec.withNewEbsVolume(new Integer(System.getProperty("cloudtools.ebs.volume.size")).intValue(), System.getProperty("cloudtools.ebs.device"));
		
			event("StatusUpdate", ["Configuring database"])
			cluster.getClusterInstance().getDatabaseTier().getDatabaseServer().configureDatabase();
		    
			event("StatusUpdate", ["Saving Cluster"])
			clusterManager.saveCluster(clusterName, cluster);
			
			event("StatusFinal", ["Done"])
		
		}else{
			event("StatusError", ["You did not specify either 'cloudtools.ebs.volume.size' or 'cloudtools.ebs.device'\nExample: grails -Dcloudtools.ebs.volume.size=10 -Dcloudtools.ebs.device=/dev/sdj cloud-tools-enable-ebs"])
		}
	}
}
